# coding=UTF-8
import os
import cv2
from PIL import Image


image_path = '/repo/smspider/images/feedsradar_app/'  # 去水印前的图片存放路径
# image_path = '/Users/cwd/feedsradar_app_watermark/'  # 去水印前的图片存放路径
mark_path = './watermark/'  # 水印蒙版存放路径
unwatermark_path = '/repo/smspider/images/feedsradar_app/un_feedsradar_app_watermark/'  # 去水印后的图片存放路径
# unwatermark_path = '/Users/cwd/un_feedsradar_app_watermark/'  # 去水印后的图片存放路径

images = os.listdir(image_path)  # 获取指定路径下的所有水印图片名
marks = os.listdir(mark_path)  # 获取所有蒙版图片名
mark_sizes = [i[:-4] for i in marks]  # 蒙版尺寸列表
fail_lis = []

for i in range(len(images)):
    try:
        image_name = images[i]
        if str(image_name).find('.DS_Store') == -1:
            print(image_name)
            img = Image.open(image_path + image_name)
            size = img.size
            width = size[0]  # 图片宽度
            height = size[1]  # 图片高度
            mark_name = str(width) + '_' + str(height) + '.jpg'  # 水印蒙版名

            if str(width) + '_' + str(height) in mark_sizes:  # 判断该尺寸的图片是否有对应蒙版存在，如果不存在则跳过
                print(i + 1, 'of', len(images))
                target = cv2.imread(image_path + image_name)  # 默认的彩色图(IMREAD_COLOR)方式读入原始图像
                mark = cv2.imread(mark_path + mark_name, cv2.IMREAD_GRAYSCALE)  # 灰度图(IMREAD_GRAYSCALE)方式读入水印蒙版图像

                # 参数：目标修复图像; 蒙版图(定位修复区域)
                result = cv2.inpaint(target, mark, 5, cv2.INPAINT_TELEA)
                cv2.imwrite(unwatermark_path + image_name, result)  # 保存去除水印后的图片
            else:
                fail_lis.append(image_name)
    except Exception as e:
        print(e)
print(fail_lis)
print('work done')

